package com.fantasy.shares;

import java.io.IOException;
import java.util.HashMap;

import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.fantasy.file.WriteTxt;
import com.fantasy.httpclient.HttpClientUtil;

public class GetSharesData {

	public static void main(String[] args) {
		//getSharesDetails("sz300321");
		//getSharesDetails("sh600507");
		//allSharesDetails();
		getOldSharesData();
	}
	
	public static void allSharesDetails(){
		String[] codes = {"sh600" , "sh601" , "sh603","sz002" , "sz000" , "sz300" };
		for (String code : codes) {
			for (int i = 0; i <= 999; i++) {
				String number = i + "";
				int j = number.length();
				for (int j2 = j; j2 < 3; j2++) {
					number = "0" + number ;
				}
				getSharesDetails(code + number);
				/*try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}*/
			}
		}
	}
	
	public static void getSharesDetails(String head){
		HttpClientUtil.newHttpGet("http://quote.eastmoney.com/" + head + ".html?from=BaiduAladdin");
		System.out.print("\r\nGet:" + head);
			String html;
			try {
				html = HttpClientUtil.executeGet("GBK");
				//System.out.println(html);
				parseSharesDetails(html);
			} catch (ParseException e) {
				System.out.println(head + " $$ ERROR : " + e.getMessage());
				//e.printStackTrace();
			} catch (IOException e) {
				System.out.println(head + " $$ ERROR : " + e.getMessage());
				// TODO Auto-generated catch block
				//e.printStackTrace();
			}
	}
	
	private static void parseSharesDetails(String html) {
		if(html.contains("您访问的页面不存在") ){
			System.out.print("不存在");
			return;
		}
		if("".equals(html)){
			return;
		}
		Document doc = Jsoup.parse(html);
		HashMap<String, String> data = new HashMap<String, String>();
		Element elem = doc.getElementById("name");
		data.put("name", elem.text());
		elem = doc.getElementById("code");
		data.put("code", elem.text());
		elem = doc.getElementById("rtp2");
		data.put("net_assets", elem.select("tr:eq(1) td:eq(0)").text().replace("净资产：", ""));
		data.put("net_rate", elem.select("tr:eq(1) td:eq(1) span").text());
		data.put("revenue", elem.select("tr:eq(2) td:eq(0)").text().replace("营收：", ""));
		data.put("An", elem.select("tr:eq(2) td:eq(1)").text().replace("同比：", ""));
		data.put("net_profit", elem.select("tr:eq(3) td:eq(0)").text().replace("净利润：", ""));
		data.put("debt_ratio", elem.select("tr:eq(5) td:eq(1)").text().replace("负债率：", ""));
		data.put("capital", elem.select("tr:eq(6) td:eq(0)").text().replace("总股本：", ""));
		data.put("tradable", elem.select("tr:eq(7) td:eq(0)").text().replace("流通股：", ""));
		data.put("market", elem.select("tr:eq(9) td:eq(0)").text().replace("上市时间：", ""));
		SaveSharesService.save(data);
	}

	public static void  getOldSharesData(){
		HttpClientUtil.newHttpGet("http://www.aigaogao.com/tools/history.html?s=600777");
		try {
			String html = HttpClientUtil.executeGet();
			parseOldSharesData(html);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void parseOldSharesData(String oldSharesData){
		Document doc = Jsoup.parse(oldSharesData);
		Elements newsHeadlines = doc.select(".data tr");
		WriteTxt.open("D:/shares/600777.txt" );
		for(Element elem : newsHeadlines){
			Elements elems = elem.select("td");
			StringBuffer str = new StringBuffer();
			for (Element element : elems) {
				str.append(element.text() + "|");
			}
			WriteTxt.write(str.toString() + "\r\n");
		}
		WriteTxt.close();
	}
}
